package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.config.WebSocketServer;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.domain.AlertRule;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.mapper.AlertRuleMapper;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.service.IAlertRuleService;
import com.zzyl.nursing.vo.AlertNotifyVo;
import com.zzyl.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 报警规则Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-16
 */
@Service
public class AlertRuleServiceImpl extends ServiceImpl<AlertRuleMapper, AlertRule> implements IAlertRuleService
{
    @Autowired
    private AlertRuleMapper alertRuleMapper;
    @Autowired
    private RedisTemplate<Object , Object> redisTemplate;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private IAlertDataService alertDataService;
    @Autowired
    private WebSocketServer webSocketServer;
  

    /**
     * 查询报警规则
     * 
     * @param id 报警规则主键
     * @return 报警规则
     */
    @Override
    public AlertRule selectAlertRuleById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警规则列表
     * 
     * @param alertRule 报警规则
     * @return 报警规则
     */
    @Override
    public List<AlertRule> selectAlertRuleList(AlertRule alertRule)
    {
        return alertRuleMapper.selectAlertRuleList(alertRule);
    }

    /**
     * 新增报警规则
     * 
     * @param alertRule 报警规则
     * @return 结果
     */
    @Override
    public int insertAlertRule(AlertRule alertRule)
    {
        return save(alertRule) ? 1 : 0;
    }

    /**
     * 修改报警规则
     * 
     * @param alertRule 报警规则
     * @return 结果
     */
    @Override
    public int updateAlertRule(AlertRule alertRule)
    {
        return updateById(alertRule) ? 1 : 0;
    }

    /**
     * 批量删除报警规则
     * 
     * @param ids 需要删除的报警规则主键
     * @return 结果
     */
    @Override
    public int deleteAlertRuleByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警规则信息
     * 
     * @param id 报警规则主键
     * @return 结果
     */
    @Override
    public int deleteAlertRuleById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 报警数据过滤
     */
    @Override
    public void alertFilter() {
        //1.查询有没有有效的报警规则
        long count = count(Wrappers.<AlertRule>lambdaQuery().eq(AlertRule::getStatus, 1));
        if (count <= 0){
            return;
        }

        //2.有有效的规则,查询所有设备上报的数据:Object:List<DeviceData>
        List<Object> deviceDataList = redisTemplate.opsForHash().values(CacheConstants.IOT_DEVICE_DATA_LATEST);

        //判断数据是否为空
        if (CollectionUtils.isEmpty(deviceDataList)){
            return;
        }
        //3.有规则,也有数据,遍历数据,一条一条的交给一个数据报警处理器处理
        deviceDataList.forEach(deviceDatas->{
            //将一条数据强转成真是类型
            List<DeviceData> deviceData = (List<DeviceData>) deviceDatas;
            //遍历集合,拿到每一条数据,调用alertFilter处理一条数据
            deviceData.forEach(data->alertFilter(data));
        });


    }

    /**
     * 针对一条报警数据的多个物模型进行处理
     * @param deviceData  一条报警数据
     */
    private void alertFilter(DeviceData deviceData) {
        //1.准备对应的报警规则
        //第一种规则:获取针对当前设备的物模型的所有报警规则
        List<AlertRule> rules1 = list(Wrappers.<AlertRule>lambdaQuery()
                .eq(AlertRule::getStatus, 1)
                .eq(AlertRule::getProductKey, deviceData.getProductKey())
                .eq(AlertRule::getIotId, -1)
                .eq(AlertRule::getFunctionId, deviceData.getFunctionId())
        );
        //第二种:获取针对当前设备的物模型本身的报警规则
                List<AlertRule> rules2 = list(Wrappers.<AlertRule>lambdaQuery()
                .eq(AlertRule::getProductKey, deviceData.getProductKey())
                .eq(AlertRule::getIotId, deviceData.getIotId())
                .eq(AlertRule::getFunctionId, deviceData.getFunctionId())
        );
        //将两组规则合并到一起
        rules1.addAll(rules2);
        //判断规则是否为空
        if (CollUtil.isEmpty(rules1)){
            return;
        }
        //遍历每一条规则,和数据进行比较
        rules1.forEach(alertRule -> deviceDataAlarmHandler(deviceData,alertRule));

    }

    /**
     * 比较数据和规则是否匹配
     * @param alertRule
     * @param deviceData
     */
    private void deviceDataAlarmHandler(DeviceData deviceData,AlertRule alertRule ) {

                // 设计沉默周期的key
        String silentPeriodKey = CacheConstants.RULE_SILENT+deviceData.getIotId()+":"+ alertRule.getFunctionId()+":"+alertRule.getId();
        //报警次数的key
        String alertCountKey = CacheConstants.RULE_COUNT+ deviceData.getIotId()+":"+ alertRule.getFunctionId()+":"+alertRule.getId();


        //1.判断设备数据是否在规则有效时间内
        LocalTime alarmTime = deviceData.getAlarmTime().toLocalTime();
        //00.00.00--23.59.59
        String[] times = alertRule.getAlertEffectivePeriod().split("~");
        LocalTime begin = LocalTime.parse(times[0], DateTimeFormatter.ofPattern("HH:mm:ss"));
        LocalTime end = LocalTime.parse(times[1], DateTimeFormatter.ofPattern("HH:mm:ss"));

        //如果设备数据上报时间不在有效范围内,直接返回
        if (alarmTime.isBefore(begin) || alarmTime.isAfter(end)){
            return;
        }
        //2.判断数据阈值
        //设备上报的数据
        Double dataValue = Double.valueOf(deviceData.getDataValue());
        //规则中的阈值
        Double value = alertRule.getValue();
        //先比较两个数据的大小关系
        //如果datavalue大于rulevalue,返回正整数,否则返回负整数,如果两数相等,返回0
        int compare = Double.compare(dataValue, value);

        //规则中的运算符
        String operator = alertRule.getOperator();

        //判断阈值
        if (">=".equals(operator) && compare < 0 || "<".equals(operator) && compare >= 0){
            // 未达到阈值,要删除redis中的报警数
            redisTemplate.delete(alertCountKey);

            return;
        }
        //设备上报的数据达到了阈值,判断redis中的沉默周期

        Object silentPeriod = redisTemplate.opsForValue().get(silentPeriodKey);
        if (ObjectUtil.isNotEmpty(silentPeriod)){
            //如果沉默周期不为空,标识在沉默周期内,不做处理
            return;
        }
        //不在沉默周期内,从redis中查询已报警次数
        Integer alarmCount = (Integer) redisTemplate.opsForValue().get(alertCountKey);
        //如果从redis中获取的已报警次数为null,设置值为1
        if (ObjectUtil.isEmpty(alarmCount)){
            alarmCount = 1;
        }else {
            alarmCount++;
        }
        //已触发报警次数加一后,判断是否等于持续周期
        if (!alertRule.getDuration().equals(alarmCount)){
            //不等于持续周期,更新redis中的已报警次数为加一后的值,返回
            redisTemplate.opsForValue().set(alertCountKey,alarmCount);
            return;
        }
        //证明达到了持续周期,可以保存这条报警数据了
        //删除redis中的报警数
        redisTemplate.delete(alertCountKey);

        //添加redis沉默周期
        redisTemplate.opsForValue().set(silentPeriodKey,1,alertRule.getAlertSilentPeriod(), TimeUnit.MINUTES);
        // 批量保存报警数据到数据库中
        List<Long> userIds = batchSaveAlertData(deviceData, alertRule);

        //将报警数据推送给所有需要报警的客户端
        sendWebSocketMsg(deviceData,alertRule,userIds); 
        
    

    }

    private void sendWebSocketMsg(DeviceData deviceData, AlertRule alertRule, List<Long> userIds) {
    AlertNotifyVo alertNotifyVo = BeanUtil.toBean(deviceData, AlertNotifyVo.class);
    alertNotifyVo.setNotifyType(1);
    alertNotifyVo.setAlertDataType(alertRule.getAlertDataType());
    alertNotifyVo.setFunctionName(alertRule.getFunctionName());

    webSocketServer.sendMessageToConsumer(alertNotifyVo, userIds);
    }

    /**
     * 批量保存报警数据到数据库中
     * @param deviceData    设备报警数据
     * @param alertRule     报警规则
     */
    private List<Long> batchSaveAlertData(DeviceData deviceData, AlertRule alertRule) {
        List<Long> userIds;
        //1.判断是否是老人数据
        if (alertRule.getAlertDataType().equals(0)){
            //老人数据
            //判断是固定设备还是随身设备
            if (deviceData.getLocationType().equals(0)){
                //随身设备
                 userIds = deviceMapper.selectNursingIdsByIotIdWithElder(deviceData.getIotId());
            }else{
                //固定设备,床位上的设备
                 userIds = deviceMapper.selectNursingIdsByIotIdWithBed(deviceData.getIotId());
            }
        }else {
            //设备数据
             userIds = sysUserRoleMapper.selectUserIdByRoleName("维修工");
        }
        //查询超级管理员的id
        List<Long> managerIds = sysUserRoleMapper.selectUserIdByRoleName("超级管理员");
//合并所有要通知的人员列表
        userIds.addAll(managerIds);
        //批量保存报警数据
        List<AlertData> alertDataList = new ArrayList<>();
        //遍历要通知的人的集合
        userIds.forEach(userId -> {
            //为当前要通知的人构建一条报警数据,先从DeviceData拷贝出一份DeviceData对象
            AlertData alertData = BeanUtil.toBean(deviceData, AlertData.class);
            //设置主键id为null
            alertData.setId(null);
            //设置要通知人的id
            alertData.setUserId(userId);
            alertData.setAlertRuleId(alertRule.getId());
            String reason = CharSequenceUtil.format("{}{}{},持续{}个周期报警", alertRule.getFunctionName(), alertRule.getOperator(), alertRule.getValue(), alertRule.getDuration());
            alertData.setAlertReason(reason);
            alertData.setType(alertRule.getAlertDataType());
            //状态为待处理
            alertData.setStatus(0);
            //将当前报警数据对象添加到集合中
            alertDataList.add(alertData);
        });


        alertDataService.saveBatch(alertDataList);
        return userIds;
    }
}





